import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-10-13
 * Time: 15:22
 */
@WebServlet("/showServlet")
public class showServlet extends HttpServlet {
    //熟悉HttpServletRequest的API使用
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //防止中文乱码
        resp.setContentType("text/html; charset=utf8");
        StringBuilder res = new StringBuilder();
        //拼接HTTP版本号
        res.append(req.getProtocol());
        res.append("<br>");
        //方法
        res.append(req.getMethod());
        res.append("<br>");
        //contextPath路径
        res.append(req.getContextPath());
        res.append("<br>");
        //servlet的路径->hello
        res.append(req.getRequestURI());
        res.append("<br>");
        //queryString
        res.append(req.getQueryString());
        res.append("<br>");
        //请求报头中的一系列东西
        Enumeration<String> headerNames= req.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            res.append(name + " : " + req.getHeader(name));
            res.append("<br>");
        }
        resp.getWriter().write(res.toString());
    }
}
